package com.mifengs.mall.widget.UploadPhotoView;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.mifengs.commonbase.util.LogUtil;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.mall.entity.ImageUpload;
import com.mifengs.mall.loader.GlideImageLoader;
import com.mifengs.mall.util.UploadImgUtil;

import java.io.File;
import java.util.ArrayList;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


/**
 * Created by lihaoqi on 2018/3/22.
 */

public class UpLoadPhotoViewController implements OnUpLoadPhotoViewListener{

    private UpLoadPhotoView mView;

    private ImagePicker mImagePicker;

    ArrayList<ImageItem> images = null;

    private UploadStatusListener mUploadListener;

    private boolean mIsUploading = false;


    public UpLoadPhotoViewController(UpLoadPhotoView view) {
        this.mView = view;
    }

    public void choosePhoto(){
        mImagePicker = ImagePicker.getInstance();
        mImagePicker.setImageLoader(new GlideImageLoader());
        mImagePicker.setMultiMode(false);
        mImagePicker.setShowCamera(true);
        mImagePicker.setCrop(false);

        Intent intent = new Intent(mView.getContext(), ImageGridActivity.class);
        //intent.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
        //ImagePicker.getInstance().setSelectedImages(images);
        if(mView.getContext() instanceof Activity){
            ((Activity) mView.getContext()).startActivityForResult(intent, 100);
        }

    }

    public void setUploadListener(UploadStatusListener uploadListener) {
        this.mUploadListener = uploadListener;
    }

    @Override
    public void setSelectedImages(ArrayList<ImageItem> images) {
        if(images.size()>0){
            ImageItem imageItem = images.get(0);
            Glide.with(mView.getContext())                             //配置上下文
                    .load(Uri.fromFile(new File(imageItem.path)))      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(mView.getPhotoView());
            uploadImg(imageItem.path);
        }
    }

    private void uploadImg(String imgUrl){
        mIsUploading = true;
        mView.setLoadingView();
        Observable.just(imgUrl)
                .flatMap(new Func1<String, Observable<ImageUpload>>() {
                    @Override
                    public Observable<ImageUpload> call(String imgFileUrl) {
                        return UploadImgUtil.upLoadCompressImg(imgFileUrl);
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ImageUpload>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mIsUploading = false;
                        mView.setFailView();
                        if(mUploadListener !=null)
                        mUploadListener.fail();
                    }

                    @Override
                    public void onNext(ImageUpload imageUpload) {
                        mIsUploading = false;
                            if(imageUpload.getMsg().equals("OK") && imageUpload.getStatus().equals("1")){
                            mView.setSuccessView();
                            String imgUrl = imageUpload.getResult().getPic();
                           // System.out.println("上传照片成功：url = " + imageUpload.getResult().getPic());
                            LogUtil.i("UpLoadPhotoViewController:","上传照片成功："+imgUrl);
                            if(mUploadListener!=null)
                            mUploadListener.success(imgUrl);
                        }else{
                            mView.setFailView();
                            if(imageUpload.getStatus().equals("-1")){
                                ToastUitl.showLong("登录信息失效，上传失败");
                            }else{
                                ToastUitl.showLong(imageUpload.getMsg());
                            }
                        }
                    }
                });
    }

    public interface UploadStatusListener{
        void success(String imageUrl);
        void fail();
    }


    public boolean isUploading() {
        return mIsUploading;
    }


}
